<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

    class payservice extends Controller {

        function payservice () 
        {
            parent::Controller();

            $this->config->load('merchant');

            $cnfg_mrchnt = array(
            'url' => $this->config->item('ecomm_server_url'),
            'keystore' => $this->config->item('cert_url'),
            'keystorepassword' => $this->config->item('cert_pass'),
            1
            );

            $this->load->library('merchant', $cnfg_mrchnt);

            $this->load->model('merchant/booking_info','booking_info');
        }

        function index(){

            //$result=file_get_contents('./certs/JJ900041keystore.pem');
            //echo $result;

            $this->do_pay(759,'rentacar');


        }

        // base method PAY
        // @param booking id

        function do_pay($booking_id,$type){

            // Rentacar - Excursions - Tours  
            // Get info about booking
            
            switch ($type) {
                case 'rentacar':                    

                    $row = $this->booking_info->car_book_info($booking_id);        
                    $table = "carbooking";
                    $car = $this->booking_info->car_details_info($row['carid']);
                    $description = $car['name'].'-'.$row['numofdays'].' day/s';

                    break;
                case 'excursion':
                
                    $row = $this->booking_info->excursion_book_info($booking_id);
                    $table = "excursionbooking";
                    $exc = $this->booking_info->excursion_details_info($row['excursions_id']);
                    $description = $exc['title'].'-'.$row['noperson'].' person/s';

                    break;
                case 'tour':
                
                    $row = $this->booking_info->tour_book_info($booking_id);
                    $table = "tourbooking";
                    $tour = $this->booking_info->tour_details_info($row['tours_id']);
                    $description = $tour['title'].'-'.$row['noperson'].' person/s';

                    break;
            }

            // Row TotalPrice DECIMAL (10,2) :: must be parserd 
            // as integer for correct system functionality
            $totalprice = $row['totalprice']*100; 


            // Klips Server -> sendTransData(amount, currency, ip, desc)  :: return (transactionId)
            $resp = $this->merchant->startDMSAuth($totalprice, $this->config->item('currency'), $_SERVER['REMOTE_ADDR'], $description, 'lv');                       


            /*$resp = 'TRANSACTION_ID: 2s8ZdE87qZBREEaWZm1O6omo6Kg=';
            echo $resp;*/

            // if response = transactionid
            if(substr($resp, 0, 14) == 'TRANSACTION_ID'){ 

                $trans_id = substr($resp, 16, 28); 

                $url = $this->config->item('ecomm_client_url')."?trans_id=". urlencode($trans_id);

                $q = "INSERT INTO ".$this->config->item('m_db_table_transaction')." VALUES ('', '$type', '$trans_id', ".$totalprice.",".$this->config->item('currency').",'".$_SERVER['REMOTE_ADDR']."' , '".$description."', '', 'NO', '???', '???', '???', '???', now(), '$resp', '', '')";
                /*echo 'q1:'.$q;
                echo '<br />';
                echo '<br />';*/
                $sql = mysql_query($q);


                if (!$sql) {
                    die('*** Invalid query1: ' . mysql_error());
                }

                //Update booking with transaction_id                    
                $q = "UPDATE ".$table." SET trans_id='".$trans_id."' WHERE id=".$booking_id;

                /*echo 'q2:'.$q;
                echo '<br />';
                echo '<br />';*/
                $sql = mysql_query($q);                   

                if (!$sql) {
                    die('*** Invalid query1: ' . mysql_error());
                }

                /*{This is the last point,
                * where Klips definetly knows,  
                * that payment is not made yet.}
                * 
                * Redirect To ECOMM
                */

                /*echo 'url:'.$url;
                echo '<br />';*/

                redirect($url); // redirect to secure server!

            }else{
                
                // Somthing went wrong
                // This is response: unable to use client certificate (no key found or wrong pass phrase?)
                //redirect('http://www.informacionisistem.com/rentacar/rentacar/payservice/do_pay/'.$booking_id.'/'.$type);
                
                echo "This is response: ".$resp;         /**     OBRADITI OVAJ SLUCAJ NA GLOBTOUR SERVERU            **/
                
            }

        }

    }

?>
